import 'package:flutter/material.dart';
import 'package:hixing_app/const/color_res.dart';
import 'package:hixing_app/const/gaps.dart';
import 'package:hixing_app/extensions/decoration_ext.dart';

class SearchInput extends StatelessWidget {
  SearchInput({
    required this.controller,
    this.color,
    this.hintText = '请输入关键词',
    this.iconWidget,
    this.autoFocus = false,
    this.onSubmitted,
    this.iconClick,
    this.paddingHorizontal,
    this.showSearchIcon = true,
    this.radius = 100,
    this.borderColor = Colors.transparent,
    this.onSearchTap,
    this.node,
    this.rightWidget,
    this.height,
    this.searchInputClick,
    this.fillColor = Colors.white,
    this.textSize,
    this.hintTextSize,
    this.textColor,
    this.keyboardType,
    this.isShowShadow = true,
    this.suffixWidget,
  });

  final TextEditingController controller;
  final Color currentColor = ColorRes.textColor;
  final Color? color;
  final bool autoFocus;
  final String hintText;
  final Color fillColor;
  final Widget? iconWidget;
  final Widget? suffixWidget;
  final FocusNode? node;
  final double? paddingHorizontal;
  final bool showSearchIcon;
  final bool isShowShadow;
  final Function()? searchInputClick;
  final Color borderColor;
  final Function()? iconClick;
  final Function(String)? onSubmitted;
  final double radius;
  final Widget? rightWidget;
  final double? textSize;
  final double? hintTextSize;
  final Color? textColor;
  final double? height;
  final Function()? onSearchTap;
  final TextInputType? keyboardType;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: paddingHorizontal ?? 16),
      child: Container(
        margin: const EdgeInsets.only(top: 10),
        decoration: isShowShadow
            ? const BoxDecoration().getShadow2
            : const BoxDecoration(),
        height: height ?? 32.0,
        child: Row(
          children: [
            iconWidget == null
                ? Container()
                : GestureDetector(
                    behavior: HitTestBehavior.opaque,
                    onTap: iconClick,
                    child: Container(
                      padding: const EdgeInsets.only(
                          left: 0, right: 10, bottom: 4, top: 4),
                      child: Row(
                        children: [iconWidget!],
                      ),
                    ),
                  ),
            Expanded(
              child: GestureDetector(
                onTap: searchInputClick,
                child: Theme(
                  data: ThemeData.dark(),
                  child: TextField(
                    key: key,
                    focusNode: node,
                    autofocus: autoFocus,
                    controller: controller,
                    onSubmitted: onSubmitted,
                    enabled: searchInputClick == null,

                    keyboardType: keyboardType,
// 光标颜色
                    cursorColor: textColor ?? Colors.white,
// 文案颜色
                    style: TextStyle(
                        color: textColor ?? Colors.red,
                        fontWeight: FontWeight.normal,
                        fontSize: textSize ?? 14.0),
                    decoration: InputDecoration(
                      filled: true,
                      enabledBorder: OutlineInputBorder(
                        borderSide: BorderSide(color: borderColor),
                        borderRadius: BorderRadius.all(Radius.circular(radius)),
                      ),
                      disabledBorder: OutlineInputBorder(
                        borderSide: BorderSide(color: borderColor),
                        borderRadius: BorderRadius.all(
                          Radius.circular(radius),
                        ),
                      ),
//添加内部图标之后，图标和文字会有间距，实现这个方法，不用写任何参数即可解决
                      prefixIconConstraints: const BoxConstraints(),
                      focusedBorder: OutlineInputBorder(
                        borderSide: BorderSide(color: borderColor),
                        borderRadius: BorderRadius.all(
                          Radius.circular(radius),
                        ),
                      ),
                      prefixIcon: GestureDetector(
                        onTap: () {
                          if (node != null && node!.hasFocus) {
                            FocusScope.of(context).requestFocus(FocusNode());
                            if (onSearchTap != null) {
                              onSearchTap!();
                            }
                          }
                        },
                        child: Container(
                          margin: const EdgeInsets.only(left: 10, right: 6),
                          child: Icon(Icons.search,
                              //AppIconR.search,
                              color: color ?? currentColor,
                              size: showSearchIcon ? textSize : 0),
                        ),
                      ),
                      suffixIcon: suffixWidget,
                      contentPadding: const EdgeInsets.only(left: 10.0),
                      fillColor: fillColor,
                      hintText: hintText,
                      hintStyle: TextStyle(
                          color: color ?? currentColor,
                          fontSize: hintTextSize ?? 14.0),
                    ),
                  ),
                ),
              ),
            ),
            rightWidget == null ? const Text('') : Gaps.hGap10,
            rightWidget ?? const Text('')
          ],
        ),
      ),
    );
  }

  Widget _buildContextMenu(BuildContext context, EditableTextState state) {
    final TextEditingValue value = state.textEditingValue;

    final List<ContextMenuButtonItem> buttonItems =
        state.contextMenuButtonItems;
    String selectValue = value.selection.textInside(value.text);

    buttonItems.insert(
        0,
        ContextMenuButtonItem(
          label: 'Send email',
          onPressed: () {},
        ));

    return AdaptiveTextSelectionToolbar.buttonItems(
      anchors: state.contextMenuAnchors,
      buttonItems: buttonItems,
    );
  }
}
